const md5 = require("md5");
const { login } = require("../model/user");

// 登录
const Login = async (ctx) => {
    const { username, password } = ctx.request.body;
    const data = await login(username, md5(password));
    if (data) {
        ctx.session.user = { username, id: data._id };
        ctx.body = {
            code: 200,
            msg: "登录成功",
            data,
        };
    } else {
        ctx.body = {
            code: 400,
            msg: "用户名或密码错误",
            data: null,
        };
    }
};

// 退出登录
const Logout = (ctx) => {
    ctx.session.user = null;
    ctx.body = {
        code: 200,
        msg: "退出成功",
        data: null,
    };
};

module.exports = { Login, Logout };
